package com.heima.controller;


import com.heima.pojo.dto.RegionQuery;
import com.heima.pojo.dto.RegionDto;
import com.heima.pojo.po.Region;
import com.heima.pojo.vo.RegionVo;
import com.heima.pojo.vo.PageVo;
import com.heima.service.IRegionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 运营区域 前端控制器
 * </p>
 *
 * @author 存折
 * @since 2024-10-18
 */

@RestController
@RequestMapping("/api/vm-service/region")
@Slf4j
public class RegionController {

    @Autowired
    private IRegionService iRegionService;

    @GetMapping("/search")
    public PageVo<RegionVo> Query(RegionQuery regionQuery) {
        return iRegionService.query(regionQuery);
    }

    @PostMapping
    public Boolean add(@RequestBody RegionDto regionDto) {
        Region region = new Region();
        region.setName(regionDto.getRegionName());
        region.setRemark(regionDto.getRemark());
        iRegionService.save(region);
        return true;
    }

    @PutMapping("{regionId}")
    public Boolean update(@PathVariable String regionId, @RequestBody RegionDto regionDto) {
        Region region = new Region();
        region.setId(Long.parseLong(regionId));
        region.setName(regionDto.getRegionName());
        region.setRemark(regionDto.getRemark());
        iRegionService.updateById(region);
        return true;
    }

    @GetMapping("{regionId}")
    public Region findDetail(@PathVariable String regionId) {
        Region region = new Region();
        region.setId(Long.parseLong(regionId));
        return iRegionService.getById(region);

    }

    @DeleteMapping("{regionId}")
    public Boolean delete(@PathVariable String regionId) {
        log.info("接受到的参数为：{}", regionId);
        iRegionService.delete(regionId);
        return true;
    }
}
